//
//  PolicyOrderListVC.m
//  NtoccShipper
//
//  Created by mac on 2019/11/8.
//  Copyright © 2019 zhuchongjin. All rights reserved.
//

#import "PolicyOrderListVC.h"
#import "PolicyOrderListCell.h"
#import "SearchPolicyListVC.h"
#import "PolicyOrderListModel.h"
#import "CJWKWebViewVC.h"
@interface PolicyOrderListVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) PolicyOrderListCell *policyOrderListCell;
@property (nonatomic,strong) CJTableView *tableView;
@property (nonatomic,strong) NSMutableArray *modelArray;
@property (nonatomic,strong) NSString *refreshType; //刷新
@property (nonatomic,readwrite) int page;
@property (nonatomic,copy) NSString *strPolicyNo;
@property (nonatomic,copy) NSString *strMainOrderNumber;
@property (nonatomic,copy) NSString *strOrderDetailNumber;
@property (nonatomic,copy) NSString *strGmtCreatedStart;
@property (nonatomic,copy) NSString *strGmtCreatedEnd;
@property (nonatomic,copy) NSString *strShipmentInfo;
@property (nonatomic,copy) NSString *strDriverInfo;
//@"policyNo":@"",
//@"mainOrderNumber":@"",
//@"orderDetailNumber":@"",
//@"gmtCreatedStart":@"",
//@"gmtCreatedEnd":@"",
//@"shipmentInfo":@"",
//@"driverInfo":@""
@end

@implementation PolicyOrderListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setTitleName:CJLString(@"保单列表")];
    [self setBackLeftButton:@""];
//    [self setSecondAppNavigationBarStyle];
    [self setDealut];
    self.policyOrderListCell = [[PolicyOrderListCell alloc]initWithFlex:nil reuseIdentifier:nil];
    [self.view addSubview:self.tableView];
    WS(ws);
    self.tableView.placeHolderView = [[KKTableViewNoDataView alloc] initWithFrame:self.tableView.bounds image:[UIImage imageNamed:@"no_data"] viewClick:^{
           [ws.tableView.mj_header beginRefreshing];
    }];
    [self initData];
    [self loadData];
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(SCREEN_WIDTH-50, kStatusBarHeight, 40,40);
    UIImageView * imageview = [[UIImageView alloc]initWithFrame:CGRectMake(15, 13, 17, 17)];
    imageview.image = [UIImage imageNamed:@"zcj_icon_search_gray"];
    //    [leftButton setImage:[[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
    [leftButton addSubview:imageview];
    [self.image addSubview:leftButton];
    leftButton.adjustsImageWhenHighlighted = NO;
    [leftButton addTarget:self action:@selector(serachBtnAction) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *leftButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton1.frame = CGRectMake(SCREEN_WIDTH-80, kStatusBarHeight, 40,40);
    UIImageView * imageview1 = [[UIImageView alloc]initWithFrame:CGRectMake(15, 14, 17.5, 16)];
    imageview1.image = [UIImage imageNamed:@"zcj_icon_refresh"];
    //    [leftButton setImage:[[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
    [leftButton1 addSubview:imageview1];
    [self.image addSubview:leftButton1];
    leftButton1.adjustsImageWhenHighlighted = NO;
    [leftButton1 addTarget:self action:@selector(btnRefreshData) forControlEvents:UIControlEventTouchUpInside];
}

- (void)setDealut{
    self.strPolicyNo = @"";
    self.strMainOrderNumber = @"";
    self.strOrderDetailNumber = @"";
    self.strGmtCreatedStart = @"";
    self.strGmtCreatedEnd = @"";
    self.strShipmentInfo = @"";
    self.strDriverInfo = @"";
}

- (void)serachBtnAction{

    WS(ws);
    SearchPolicyListVC *vc = [[SearchPolicyListVC alloc]init];
    vc.blockSerach = ^(id anyObject) {
        NSDictionary *dic = (NSDictionary *)anyObject;
        ws.strPolicyNo = dic[@"policyNo"];
        ws.strMainOrderNumber = dic[@"mainOrderNumber"];
        ws.strOrderDetailNumber = dic[@"orderDetailNumber"];
        ws.strGmtCreatedStart = dic[@"gmtCreatedStart"];
        ws.strGmtCreatedEnd = dic[@"gmtCreatedEnd"];
        ws.strShipmentInfo = dic[@"shipmentInfo"];
        ws.strDriverInfo = dic[@"driverInfo"];
        
        [ws.tableView.mj_header beginRefreshing];
    };
    [self pushVc:vc];

}
- (void)btnRefreshData{
    [self setDealut];
    [self.tableView.mj_header beginRefreshing];
}
- (void)initData{
    self.modelArray = [[NSMutableArray alloc]init];
    self.page = 1;
}

/// <#Description#>
- (void)loadData {

    NSString *strUrl = CJStringWithFormat(@"%@/ntocc-basic-api/openInsure",CJBaseUrl);
    NSDictionary *dicPra = @{@"pageNum":CJStringWithFormat(@"%d",self.page),
                             @"pageSize":@"10",
                             @"policyNo":self.strPolicyNo,
                             @"mainOrderNumber":self.strMainOrderNumber,
                             @"orderDetailNumber":self.strOrderDetailNumber,
                             @"gmtCreatedStart":self.strGmtCreatedStart,
                             @"gmtCreatedEnd":self.strGmtCreatedEnd,
                             @"shipmentInfo":self.strShipmentInfo,
                             @"driverInfo":self.strDriverInfo
                     
    };
    WS(ws);
    [[HDNetworking sharedHDNetworking] GET:strUrl parameters:dicPra success:^(id  _Nonnull responseObject) {

        DLog(@"++++++++%@",responseObject);
        NSArray *list = responseObject[@"records"];
        NSString *total = CJStringWithFormat(@"%@",responseObject[@"total"]);
        if (list.count > 0) {

           if ([ws.refreshType isEqualToString:@"down"]) {
               [ws.modelArray removeAllObjects];
           }

           NSError * error ;
           NSArray *arr = [PolicyOrderListModel arrayOfModelsFromDictionaries:list error:&error];
           [ws.modelArray addObjectsFromArray:arr];

           [ws.tableView.mj_header endRefreshing];
           [ws.tableView.mj_footer endRefreshing];

           if (ws.modelArray.count == total.intValue) {
               [ws.tableView.mj_footer endRefreshingWithNoMoreData];
           }

        }else{
//           [ws.modelArray removeAllObjects];
           [ws.tableView.mj_header endRefreshing];
           [ws.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        [ws.tableView reloadData];

    } failure:^(NSError * _Nonnull error) {
        DLog(@"++++++++%@",error);
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    }];
}

- (void)downRefresh{
    self.refreshType = @"down";
    self.page = 1;
    [self loadData];
}

- (void)upRefresh{
    self.refreshType = @"up";
    self.page ++;
    [self loadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identifier = @"PolicyOrderListCell";
    PolicyOrderListCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[PolicyOrderListCell alloc] initWithFlex:nil reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
    PolicyOrderListModel *model = self.modelArray[indexPath.row];
    [cell setCellInfoWithModel:model];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    PolicyOrderListModel *model = self.modelArray[indexPath.row];
    if ([CJRegular getIsStrEmpty:model.openInsurePDF]) {
    }else{
        CJWKWebViewVC *vc = [CJWKWebViewVC new];
        vc.strUrl = model.openInsurePDF;
        vc.strTitle = @"保单详情";
        [self pushVc:vc];
    }
}

- (CJTableView *)tableView{
    if (!_tableView) {
        _tableView = CJTableView.tbInitFrameStyle(CGRectMake(0, kTopHeight + CJTableToTopHeight, SCREEN_WIDTH, SCREEN_HEIGHT-kTopHeight - CJTableToTopHeight),UITableViewStylePlain)
        .tbDelegate(self)
        .tbDataSource(self)
        .tbSetOther(YES)
        .tbEstimatedRowHeight(YES)
        .tbMjHeadFooter(YES)
        ;
        WS(ws);
        _tableView.blockMjHeader = ^(BOOL isheader) {
            
            if (isheader) {
                [ws downRefresh];
            } else {
                [ws upRefresh];
            }
        };
    }
    return _tableView;
}


@end
